﻿using System;
using System.Linq;
using FlexCel.Core;
using System.Collections.Generic;
using InfoSoftGlobal;


namespace TVC.Modules.Ddb
{
    public class MyChart
    {
        DTO.LIST_CHARTInfo _chartInf = new DTO.LIST_CHARTInfo();

        public DTO.LIST_CHARTInfo ChartInf
        {
            get { return _chartInf; }
            set { _chartInf = value; }
        }

        public String DTB { get { return _chartInf.DTB; } set { _chartInf.DTB = value; } }
        public Int32 ID { get { return _chartInf.ID; } set { _chartInf.ID = value; } }
        public Int32 IDGadget { get { return _chartInf.IDGadget; } set { _chartInf.IDGadget = value; } }
        public String SheetChart { get { return _chartInf.SheetChart; } set { _chartInf.SheetChart = value; } }
        public String NameRange { get { return _chartInf.NameRange; } set { _chartInf.NameRange = value; } }
        public String CaptionRange { get { return _chartInf.CaptionRange; } set { _chartInf.CaptionRange = value; } }
        public String SubCaptionRange { get { return _chartInf.SubCaptionRange; } set { _chartInf.SubCaptionRange = value; } }
        public String ChartType { get { return _chartInf.ChartType; } set { _chartInf.ChartType = value; } }
        public String PositionReplace { get { return _chartInf.PositionReplace; } set { _chartInf.PositionReplace = value; } }
        public Int32 width { get { return _chartInf.width; } set { _chartInf.width = value; } }
        public Int32 height { get { return _chartInf.height; } set { _chartInf.height = value; } }

        String _dataxml = "";
        String _codechart = "";


        public String Codechart
        {
            get { return _codechart; }
            set { _codechart = value; }
        }

        public String Dataxml
        {
            get { return _dataxml; }
            set { _dataxml = value; }
        }

        public MyChart()
        {
            _dataxml = "";
            _codechart = "";
        }

        public String GetCodeChart(ExcelFile _xlsFile)
        {
            int XF = 0;

            int currentSheet = _xlsFile.ActiveSheet;
            int indexSheet = _xlsFile.GetSheetIndex(SheetChart);
            _xlsFile.ActiveSheet = indexSheet;
            TXlsNamedRange range = _xlsFile.GetNamedRange(NameRange, indexSheet);
            string attribute = "";
            _dataxml = "<chart>";


            TXlsNamedRange rangeCaption = _xlsFile.GetNamedRange(CaptionRange, indexSheet);
            if (rangeCaption != null && rangeCaption.IsOneCell)
            {
                string caption = _xlsFile.GetCellValue(indexSheet, rangeCaption.Top, rangeCaption.Left, ref XF) == null ? "" : _xlsFile.GetCellValue(indexSheet, rangeCaption.Top, rangeCaption.Left, ref XF).ToString();
                if (caption.Trim() != "")
                    attribute += String.Format(" caption='{0}'", XmlEncoder.Decode(caption));
            }

            TXlsNamedRange rangesubCaption = _xlsFile.GetNamedRange(SubCaptionRange, indexSheet);
            if (rangesubCaption != null && rangesubCaption.IsOneCell)
            {
                string caption = _xlsFile.GetCellValue(indexSheet, rangesubCaption.Top, rangesubCaption.Left, ref XF) == null ? "" : _xlsFile.GetCellValue(indexSheet, rangesubCaption.Top, rangesubCaption.Left, ref XF).ToString();
                if (caption.Trim() != "")
                    attribute += String.Format(" subCaption='{0}'", XmlEncoder.Decode(caption));
            }


            if (range.ColCount == 2)
            {
                string namex = _xlsFile.GetCellValue(indexSheet, range.Top, range.Left, ref XF) == null ? "" : _xlsFile.GetCellValue(indexSheet, range.Top, range.Left, ref XF).ToString();
                string namey = _xlsFile.GetCellValue(indexSheet, range.Top, range.Left + 1, ref XF) == null ? "" : _xlsFile.GetCellValue(indexSheet, range.Top, range.Left + 1, ref XF).ToString();
                if (namex != "")
                    attribute += String.Format(" xAxisName='{0}'", XmlEncoder.Decode(namex));
                if (namey != "")
                    attribute += String.Format(" yAxisName='{0}'", XmlEncoder.Decode(namey));


                for (int i = 1; i < range.RowCount - 1; i++)
                {


                    string name = _xlsFile.GetCellValue(indexSheet, range.Top + i, range.Left, ref XF) == null ? "" : _xlsFile.GetCellValue(indexSheet, range.Top + i, range.Left, ref XF).ToString();
                    string value = _xlsFile.GetCellValue(indexSheet, range.Top + i, range.Left + 1, ref XF) == null ? "" : _xlsFile.GetCellValue(indexSheet, range.Top + i, range.Left + 1, ref XF).ToString();
                    string temp = String.Format("<set label ='{0}' value='{1}' />", XmlEncoder.Decode(name), value);
                    _dataxml += temp;

                }

            }

            else if (range.ColCount > 2)
            {
                #region DataLable
                _dataxml += "<categories>";


                for (int i = 1; i < range.RowCount - 1; i++)
                {
                    //<category label='Austria' /> 
                    string value = _xlsFile.GetCellValue(indexSheet, range.Top + i, range.Left, ref XF) == null ? "" : _xlsFile.GetCellValue(indexSheet, range.Top + i, range.Left, ref XF).ToString();
                    string temp = String.Format("<category label ='{0}'/>", value);
                    _dataxml += temp;

                }
                _dataxml += "</categories>";
                #endregion

                for (int i = 1; i < range.ColCount; i++)
                {
                    //<dataset seriesName='1997' color='F6BD0F' showValues='0'>
                    string seriesName = _xlsFile.GetCellValue(indexSheet, range.Top, range.Left + i, ref XF) == null ? "" : _xlsFile.GetCellValue(indexSheet, range.Top, range.Left + i, ref XF).ToString();
                    _dataxml += String.Format("<dataset seriesName='{0}'>", XmlEncoder.Encode(seriesName));

                    for (int j = 1; j < range.RowCount - 1; j++)
                    {
                        //<set label="Item A" value="4" />
                        //string comment = "";
                        //comment = _xlsFile.GetComment(range.Top + j, range.Left + i).ToString();
                        //comment = XmlEncoder.Encode(comment);
                        //comment = XmlEncoder.Decode(comment);
                        //if (comment.Contains("TT_XLB_EB"))
                        //    comment = " link='tvcqd:" + comment + "'";
                        string value = _xlsFile.GetCellValue(indexSheet, range.Top + j, range.Left + i, ref XF) == null ? "" : _xlsFile.GetCellValue(indexSheet, range.Top + j, range.Left + i, ref XF).ToString();
                        string temp = String.Format("<set value='{0}' />", XmlEncoder.Decode(value));
                        _dataxml += temp;
                    }
                    _dataxml += "</dataset>";
                }
            }
            _xlsFile.ActiveSheet = currentSheet;
            _dataxml = _dataxml.Insert(_dataxml.IndexOf("<chart>") + 6, attribute);
            _dataxml += "</chart>";
            //          _dataxml = "<chart caption='Country Comparison' showLabels='1' showvalues='0' decimals='0' numberPrefix='$'><categories><category label='Austria' /><category label='Brazil' /><category label='France' /><category label='Germany' /><category label='USA' /></categories><dataset seriesName='1996' color='AFD8F8' showValues='0'><set value='25601.34' /><set value='20148.82' /><set value='17372.76' /><set value='35407.15' /><set value='38105.68' /></dataset><dataset seriesName='1997' color='F6BD0F' showValues='0'><set value='57401.85' /><set value='41941.19' /><set value='45263.37' /><<set value='114845.27' /></dataset><dataset seriesName='1998' color='8BBA00' showValues='0'><set value='45000.65' /><set value='44835.76' /><set value='18722.18' /><set value='77557.31' /><set value='92633.68' /></dataset></chart>";
            if (ChartType != "")
                _codechart = FusionCharts.RenderChartHTML(String.Format("../FusionCharts/{0}.swf", ChartType), "", _dataxml, "myNext", "600", "300", false);
            else
                _codechart = FusionCharts.RenderChartHTML("../FusionCharts/Column3D.swf", "", _dataxml, "myNext", "600", "300", false);

            return _codechart;
        }

        public MyChart(string typechart, string sheetchart, string namerange, string captionrange, string subcaptionrange)
        {
            _dataxml = "";
            _codechart = "";
            SheetChart = sheetchart;
            NameRange = namerange;
            CaptionRange = captionrange;
            SubCaptionRange = subcaptionrange;
            ChartType = typechart;
        }
    }
}